﻿BEGIN
  FOR cur IN (SELECT table_name FROM USER_TABLES WHERE table_name = 'MULTIKEY') LOOP
	EXECUTE IMMEDIATE 'DROP TABLE MULTIKEY CASCADE CONSTRAINTS';
  END LOOP;

  FOR cur IN (SELECT sequence_name FROM USER_SEQUENCES WHERE sequence_name = 'MULTIKEY_SEQ') LOOP
    EXECUTE IMMEDIATE 'DROP SEQUENCE MULTIKEY_SEQ';
  END LOOP;

  EXECUTE IMMEDIATE 'CREATE SEQUENCE MULTIKEY_SEQ START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE';

  EXECUTE IMMEDIATE 'CREATE TABLE MultiKey (
					   Key1 NUMBER NOT NULL,
					   Key2 VARCHAR2(50) NOT NULL,
					   Value VARCHAR2(50),
                       CONSTRAINT PK_MULTIKEY PRIMARY KEY(Key1, Key2))';

  EXECUTE IMMEDIATE 'CREATE or REPLACE TRIGGER trg#multikey#b_ins
                     BEFORE INSERT ON MULTIKEY FOR EACH ROW
                     BEGIN
                       SELECT MULTIKEY_SEQ.nextval INTO :NEW.Key1 FROM dual;
                     END;';
END;
